package at.fhj.swd.pse.beans;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import at.fhj.swd.pse.domain.UserService;
import at.fhj.swd.pse.entities.User;

/**
 * @author Florian Maderbacher
 **/

@Named
@SessionScoped
public class LoginBean implements Serializable {
	private static final long serialVersionUID = 1L;

	@Inject
	private UserService userService;
	
	@Inject
	private UserBean userBean;

	@Named
	private User loggedInUser;

	public LoginBean() {
		// TODO Auto-generated constructor stub
	}

	public void logout() {
		this.setLoggedInUser(null);
	}

	public User getLoggedInUser() {
		return this.loggedInUser;
	}

	private void setLoggedInUser(final User loggedInUser) {
		this.loggedInUser = loggedInUser;
	}

	public boolean isLoggedIn() {
		return (this.loggedInUser != null);
	}

	public boolean loginWithCredentials(final String username, final String password) {
		User foundUser = this.userService.login(username, password);
		if (foundUser != null) {
			this.setLoggedInUser(foundUser);
			this.userBean.setUser();
			return true;
		} else {
			return false;
		}
	}
}
